﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace src.TestCon
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = @"https://api.weixin.qq.com/cgi-bin/media/upload?access_token=18_gThaXtmV5fw3GWJvgpg_H-002Xl9J3UQ7ub78vHieR9O3zh-shFfPkEZM1I3wNQXO-b9udOKrUKGBIa4_vzJgHTlmuRjz6U2dyE6pUZSn0M6kT4bB13bzljs9jwdV9H2w7w-ewT9WFJGVf-IOLLfAHAIFF&type=image";
            HttpClient httpClient = new HttpClient();
            string boundary = DateTime.Now.Ticks.ToString("X");
            MultipartFormDataContent content = new MultipartFormDataContent(boundary);
            string filePath = @"C:\Users\10568\Desktop\后台设计图\后台设计图\登录.jpg";
            byte[] data = File.ReadAllBytes(filePath);
            content.Headers.Remove("Content-Type");
            content.Headers.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary);
            var contentByte = new ByteArrayContent(data);
            content.Add(contentByte);
            contentByte.Headers.TryAddWithoutValidation("Content-Disposition", $"form-data; name=\"media\";filename=\"{Path.GetFileName(filePath)}\"");
            //contentByte.Headers.Remove("Content-Type");
            //contentByte.Headers.TryAddWithoutValidation("Content-Type", "image/png");
            string str= httpClient.PostAsync(url, content).Result.Content.ReadAsStringAsync().Result;
            Console.WriteLine(str);
        }
    }
}
